
// 在插件初始化时检查是否有缓存
chrome.runtime.onInstalled.addListener(() => {
    chrome.storage.local.get(['fillPdf', 'fillDropdown'], function (result) {
        // 如果没有缓存值，设置默认选中
        if (result.fillPdf === undefined) {
            chrome.storage.local.set({ fillPdf: true });  // 默认选中
        }
        if (result.fillDropdown === undefined) {
            chrome.storage.local.set({ fillDropdown: true });  // 默认选中
        }
    });
});


// 创建右键菜单
chrome.runtime.onInstalled.addListener(() => {
    console.log('Extension installed, background script loaded');
    // 创建右键菜单
    chrome.contextMenus.create({
        id: "fillForm",
        title: "填充表单",
        contexts: ["page"]
    });
});

// 监听右键菜单点击事件
chrome.contextMenus.onClicked.addListener((info, tab) => {
    if (info.menuItemId === "fillForm" && tab.id) {
        chrome.storage.local.get(["config"], function (result) {
            const configData = result.config || [];
            console.log("Sending message to tab:", tab.id);  // 调试日志
            chrome.tabs.sendMessage(tab.id, { action: "fillForm", config: configData }, (response) => {
                if (chrome.runtime.lastError) {
                    console.log('Error sending message:', chrome.runtime.lastError);
                } else {
                    console.log('Message sent successfully');
                }
            });
        });
    } else {
        console.log("Invalid tab id or menu item clicked");
    }
});
